<?php
                                                                                                                                                                                                                                                                                                                                        
// TTTTTTTTTTTTTTTTTTTTTTT                  MMMMMMMM               MMMMMMMM                                CCCCCCCCCCCCMMMMMMMM               MMMMMMMM  SSSSSSSSSSSSSSS 
// T:::::::::::::::::::::T                  M:::::::M             M:::::::M                             CCC::::::::::::M:::::::M             M:::::::MSS:::::::::::::::S
// T:::::::::::::::::::::T                  M::::::::M           M::::::::M                           CC:::::::::::::::M::::::::M           M::::::::S:::::SSSSSS::::::S
// T:::::TT:::::::TT:::::T                  M:::::::::M         M:::::::::M                          C:::::CCCCCCCC::::M:::::::::M         M:::::::::S:::::S     SSSSSSS
// TTTTTT  T:::::T  TTTTTppppp   ppppppppp  M::::::::::M       M::::::::::M   eeeeeeeeeeee          C:::::C       CCCCCM::::::::::M       M::::::::::S:::::S            
//         T:::::T       p::::ppp:::::::::p M:::::::::::M     M:::::::::::M ee::::::::::::ee       C:::::C             M:::::::::::M     M:::::::::::S:::::S            
//         T:::::T       p:::::::::::::::::pM:::::::M::::M   M::::M:::::::Me::::::eeeee:::::ee     C:::::C             M:::::::M::::M   M::::M:::::::MS::::SSSS         
//         T:::::T       pp::::::ppppp::::::M::::::M M::::M M::::M M::::::e::::::e     e:::::e     C:::::C             M::::::M M::::M M::::M M::::::M SS::::::SSSSS    
//         T:::::T        p:::::p     p:::::M::::::M  M::::M::::M  M::::::e:::::::eeeee::::::e     C:::::C             M::::::M  M::::M::::M  M::::::M   SSS::::::::SS  
//         T:::::T        p:::::p     p:::::M::::::M   M:::::::M   M::::::e:::::::::::::::::e      C:::::C             M::::::M   M:::::::M   M::::::M      SSSSSS::::S 
//         T:::::T        p:::::p     p:::::M::::::M    M:::::M    M::::::e::::::eeeeeeeeeee       C:::::C             M::::::M    M:::::M    M::::::M           S:::::S
//         T:::::T        p:::::p    p::::::M::::::M     MMMMM     M::::::e:::::::e                 C:::::C       CCCCCM::::::M     MMMMM     M::::::M           S:::::S
//       TT:::::::TT      p:::::ppppp:::::::M::::::M               M::::::e::::::::e                 C:::::CCCCCCCC::::M::::::M               M::::::SSSSSSS     S:::::S
//       T:::::::::T      p::::::::::::::::pM::::::M               M::::::Me::::::::eeeeeeee          CC:::::::::::::::M::::::M               M::::::S::::::SSSSSS:::::S
//       T:::::::::T      p::::::::::::::pp M::::::M               M::::::M ee:::::::::::::e            CCC::::::::::::M::::::M               M::::::S:::::::::::::::SS 
//       TTTTTTTTTTT      p::::::pppppppp   MMMMMMMM               MMMMMMMM   eeeeeeeeeeeeee               CCCCCCCCCCCCMMMMMMMM               MMMMMMMMSSSSSSSSSSSSSSS   
//                        p:::::p                                                                                                                                       
//                        p:::::p                                                                                                                                       
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       ppppppppp                                                                                                                                      
                                                                                                                                                                     
//  _____      __  __         ____ __  __ ____  
// |_   __ __ |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
//   | || '_ \| |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
//   | || |_) | |  | |  __/ | |___| |  | |___) |    | WECHAT: he4966
//   |_|| .__/|_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2021/09/2
//      |_|                                         | TpMeCMS

namespace app\api\controller;
use app\api\controller\Tpmecms;
use think\Db;
use think\Config;
use Yurun\Util\HttpRequest;


use Yurun\Util\YurunHttp;
// use Yurun\Util\HttpRequest;


/**
 * 系统消息推送在线测试🚚
 * 
 */
class Sysmsg extends Tpmecms
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    public function _initialize()
    {
        parent::_initialize();
        // $this->user = $this->auth->getUserinfo();

        // if($this->auth->group_id==1){
        //     $this->error('普通用户！非司机');
        // }
        
    }

    /**
     * 推送消息给某用户
     *
     * @param string $uid 用户
     * @param string $type 类型:1=新普通发货,2=新城市快运,3=新拉货搬家,4=新顺风车,6=接单消息,7=坐标位置,8=用户消息,9=系统消息,10=订单状态修改
     * @param string $content 内容
     * @param string $order_id 订单id
     * @param string $order_status 订单类型:1=普通发货,2=城市快运,3=拉货搬家,4=顺风车
     * @return void
     */
    public function SendSysMsgUser($uid=null,$type=null,$content=null,$order_id=null,$order_status=null)
    {
        $data['uid'] = $uid;
        $data['type'] = $type;
        $data['content'] = $content;
        $data['order_id'] = $order_id;
        $data['order_status'] = $order_status;


        $url = Config::get('site.ws_url').'/send_sys_msg_user?'.http_build_query($data, '', '&');
        
        $this->success('ok',json_decode(file_get_contents($url),true));
    }


    /**
     * 推送消息给全部用户
     *
     * @param string $type
     * @param string $content
     * @return void
     */
    public function SendSysMsgAll($type=null,$content=null)
    {
       
        $data['type'] = $type;
        $data['content'] = $content;


        $url = Config::get('site.ws_url').'/send_sys_msg?'.http_build_query($data, '', '&');

        $this->success('ok',json_decode(file_get_contents($url),true));
    }




    /**
     * 修改系统消息状态（已读）
     *
     * @return void
     */
    public function UpdateSysMsgStatus($id)
    {
        Db::name('sys_msg')->where('id',$id)->where('msg_status','1')->update(['msg_status'=>'2','read_time'=>time()]);
    }

    /**
     * 获取系统消息列表
     *
     * @param string $msg_status 消息:1=未读,2=已读
     * @param int $uid 用户id
     * @param int $page 1
     * @param int $limit 10
     * @return void
     */
    public function GetSysMsgList($msg_status=null,$uid=null,$page=1,$limit=10)
    {
        if(!$uid){
            $uid = $this->auth->id;
        }
        $where['uid'] = $uid;
        if($msg_status){
            $where['msg_status'] = $msg_status; 
        }
        
        $list = Db::name('sys_msg')
            
            ->where($where)
            ->page($page,$limit)
            ->order('send_time desc')
            ->select();
        $this->success('ok',$list);
    }

    /**
     * 查看系统消息
     *
     * @param int $id 系统消息id
     * @return void
     */
    public function LookSysMsgData($id)
    {
        $this->UpdateSysMsgStatus($id);
        $this->success('ok',Db::name('sys_msg')->where('id',$id)->find());
    }



}